<?php
defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * 图片处理类
 * 主要负责用来处理商品图片和图片上传之类的
 */
class Image extends SHOPS_Controller{

    public function __construct(){
        parent::__construct();
        $this->load->model('goods_mdl');
        $this->load->model('image_mdl');
    }

    public function update(){
        $gid = $this->input->get('gid',TRUE);
        $gid = intval($gid);
        if(empty($gid)){
            show_404();
            exit;
        }

        //查询该数据是否属于该商铺
        $this->db->where('id',$gid);
        $this->db->where('status !=',0);
        $this->db->where('store_id',self::$store['id']);
        $this->db->select('id');
        $goods = $this->goods_mdl->get_one();
        if(empty($goods)){
            show_error('参数错误！');
        }

        $this->db->where('id',$gid);
        $res = $this->goods_mdl->get_one();
        $data['res'] = $res;

        if(empty($res)){
            show_error('参数错误！');
        }

        //查询图片

        $this->db->where('goods_id',$gid);
        $this->db->order_by('sort DESC,id ASC');
        $data['list'] = $this->image_mdl->select();

        $this->_view('image_update',$data);
    }

    /**
     * php 文件上传类
     */
    public function uploaded(){

        if(!$this->input->is_ajax_request()){
            show_404();
            exit;
        }

        if(empty($_FILES)){
            die('请选择文件上传');
        }

        $gid = $this->input->post('gid',TRUE);
        $gid = intval($gid);
        if(empty($gid)){
            die('参数错误!');
        }

        /** 七张图限制 */
        $this->db->where('goods_id',$gid);
        $count_res = $this->image_mdl->img_count();

        if($count_res >= 7){
            die('图片上传到达限制值！');
        }


        $title = $this->input->post('title',TRUE);
        $title = trim($title);


        $data = [
            'path' => sprintf('./upload/goods/%d',$gid)
        ];

        $path = './upload/goods/'.$gid;
        $config = [
            'upload_path' => $path.'/original/',  //上传目录
            'allowed_types' => 'gif|jpg|png',   //允许上传格式
            'max_size' => '1024',    //允许上传文件最大值 单位为 kb
            'max_width' => '2048', //文件最大宽度
            'max_height' => '1024',   //文件最大高度
            'file_name' => date('YmdHis').$gid,
            'min_width' => '200',
            'min_height' => '200'
        ];

        //上传文件路径
        if(!file_exists($config['upload_path'])){
            mkdir($config['upload_path'],0777,true);
        }

        //加载上传类
        $this->load->library('upload', $config);

        if ( ! $this->upload->do_upload('smallimg')){
            $error = array('error' => $this->upload->display_errors());
            echo $error['error'];
            exit;
        }

        $data = array('upload_data' => $this->upload->data());


        //设定裁剪成正方形需要裁剪的比例


        $tmp = [
            'img_path' => $config['upload_path'].$data['upload_data']['file_name'],
            'path' => $path,
            'file_name' => $data['upload_data']['file_name']
        ];

        $res = $this->compression($tmp);

        if(empty($title)){
            $title = $data['upload_data']['client_name'];
            $title = mb_substr($title,0,strpos($title,'.'));
        }

        //添加数据
        $datas = [
            'goods_id' => $gid,
            'name' => $title,
            'path' => mb_substr($tmp['path'],1),
            'file_name' => '/original/'.$data['upload_data']['file_name'],
            'thumb' => '/thumb/thumb_'.$data['upload_data']['file_name'],
        ];

        $r = $this->image_mdl->create($datas);
        echo $r === true ? '1' : '图片添加失败！';
    }

    /**
     * 图片压缩类
     */
    private function compression($data){
        $this->load->library('thumb_handler');

        $this->thumb_handler->setSrcImg($data['img_path']);
        $this->thumb_handler->setCutType(1);
        $this->thumb_handler->setDstImg($data['path'].'/thumb/thumb_'.$data['file_name']);
        //$this->thumb_handler->setMaskWord("test");

        // 指定固定宽高
        $this->thumb_handler->createImg(200,200);

    }

    /**
     * 图片删除
     */
    public function del_image(){

        if(!$this->input->is_ajax_request()){
            show_404();
            exit;
        }

        $id = $this->input->post('iid',TRUE);
        $id = intval($id);

        if(empty($id)){
            die('参数错误！');
        }

        $this->db->where('id',$id);
        $res = $this->image_mdl->get_one();
        if(empty($res)){
            die('参数错误！');
        }

        $res['path'] = '.'.$res['path'];

        //判断文件是否存在
        if(file_exists($res['path'].$res['file_name'])){
            @unlink($res['path'].$res['file_name']);
        }
        if(file_exists($res['path'].$res['file_name'])){
            @unlink($res['path'].$res['thumb']);
        }
        //删除数据
        $this->db->where('id',$id);
        $res = $this->image_mdl->delete();
        echo $res === true ? '1' : '服务器处理失败！';
    }

    /**
     * 设置用户排序
     */
    public function set_sort(){
        if(!$this->input->is_ajax_request()){
            show_404();
            exit;
        }

        $iid = $this->input->post('iid',TRUE);
        $id = intval($iid);

        if(empty($id)){
            die('参数错误！');
        }

        $val = $this->input->post('val',TRUE);
        $val = intval($val);
        
        $this->db->where('id',$id);
        $res = $this->image_mdl->update(['sort'=>$val]);
        echo $res === TRUE ? '1' : '修改排序失败！';
    }

}